#include "EutranConfigData.h"
#include "eutranManager.h"

EutranConfigData::EutranConfigData()
{
    for(uint8_t i=0; i<BbuManager::MAX_BBU_INSTANCES; i++)
    {
        rssiThreshold_[i] = 100;
        txPowerDb_[i] = 40;
        rxPower_[i] = 100;
        gpsTmDelay_[i] = 0;
        syncMode_[i] = 0;
        isSaveFS_[i] = false;
        isMeasEnable_[i] = false;
        workingBand_[i] = 38;
        bandWidth_[i] = 10;
        workingMode_[i] = 0;
        minRxLev_[i] = -126;
        minRxLevOffset_[i] = 1; 

    }
}

EutranConfigData* EutranConfigData::instance()
{
    static EutranConfigData instance_;
    return &instance_;
}

EutranConfigData* EutranConfigData::setRssiThreshold(uint8_t slotId, uint16_t rssi)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }

    if( rssi<=128 )
    {
        EMLogInfo("BBU[%d]: RSSI threshold changes from[%d] to [%d]", static_cast<uint32_t>(slotId), rssiThreshold_[slotId-1], rssi);
        rssiThreshold_[slotId-1] = rssi;
    }
    else
    {
        EMLogError("RSSI[%d] is out of range", rssi);
    }
    return this;
}

uint16_t EutranConfigData::getRssiThreshold(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 100;
    }
    return rssiThreshold_[slotId-1];
}

EutranConfigData* EutranConfigData::setBandWidth(uint8_t slotId, uint8_t bw)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: Bandwidth changes from[%d] to [%d]", static_cast<uint32_t>(slotId), bandWidth_[slotId-1], bw);
    bandWidth_[slotId-1] = bw;

    return this;
}

uint16_t EutranConfigData::getBandWidth(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 100;
    }
    return bandWidth_[slotId-1];
}

EutranConfigData* EutranConfigData::setTxPowerDb(uint8_t slotId, int8_t db)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: TX power DB changes from[%d] to [%d]", static_cast<uint32_t>(slotId), 
              static_cast<int32_t>(txPowerDb_[slotId-1]), static_cast<int32_t>(db));
    txPowerDb_[slotId-1] = db;

    return this;
}

int8_t EutranConfigData::getTxPowerDb(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 100;
    }
    return txPowerDb_[slotId-1];
}

EutranConfigData* EutranConfigData::setRxPower(uint8_t slotId, uint8_t pw)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: RX power changes from[%d] to [%d]", static_cast<uint32_t>(slotId), rxPower_[slotId-1], pw);
    rxPower_[slotId-1] = pw;

    return this;
}

uint16_t EutranConfigData::getRxPower(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 100;
    }
    return rxPower_[slotId-1];
}

EutranConfigData* EutranConfigData::setGpsDelay(uint8_t slotId, int32_t gpsTmDelay)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: GPS time delay changes from[%d] to [%d]", static_cast<uint32_t>(slotId), gpsTmDelay_[slotId-1], gpsTmDelay);
    gpsTmDelay_[slotId-1] = gpsTmDelay;
    return this;
}

int32_t EutranConfigData::getGpsDelay(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return gpsTmDelay_[slotId-1];
}

EutranConfigData* EutranConfigData::setSyncMode(uint8_t slotId, uint8_t mode)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: SYNC mode changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(syncMode_[slotId-1]), static_cast<uint32_t>(mode));
    syncMode_[slotId-1] = mode;
    return this;
}

uint8_t EutranConfigData::getSyncMode(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return syncMode_[slotId-1];
}

EutranConfigData* EutranConfigData::setIsSaveFS(uint8_t slotId, bool isSv)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: FS changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(isSaveFS_[slotId-1]), isSv);
    isSaveFS_[slotId-1] = isSv;
    return this;
}


EutranConfigData* EutranConfigData::setIsMeasEnable(uint8_t slotId, bool Mea)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: Measurement changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(isMeasEnable_[slotId-1]), Mea);
    isMeasEnable_[slotId-1] = Mea;
    return this;
}

bool EutranConfigData::getIsMeasEnable(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return isMeasEnable_[slotId-1];
}


bool EutranConfigData::getIsSaveFS(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return isSaveFS_[slotId-1];
}

EutranConfigData* EutranConfigData::setWorkingBand(uint8_t slotId, uint8_t band)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: Working band changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(workingBand_[slotId-1]), band);
    workingBand_[slotId-1] = band;
    return this;
}

uint8_t EutranConfigData::getWorkingBand(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return workingBand_[slotId-1];
}

EutranConfigData* EutranConfigData::setWorkingMode(uint8_t slotId, uint8_t workMode)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: Working mode changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(workingMode_[slotId-1]), workMode);
    workingMode_[slotId-1] = workMode;
    return this;
}

uint8_t EutranConfigData::getWorkingMode(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return workingMode_[slotId-1];
}

EutranConfigData* EutranConfigData::setMinRxLevOffset(uint8_t slotId, uint8_t minRxLevOffset)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: minRxLevOffset changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(minRxLevOffset_[slotId-1]), minRxLevOffset);
    minRxLevOffset_[slotId-1] = minRxLevOffset;
    return this;
}

uint8_t EutranConfigData::getMinRxLevOffset(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return minRxLevOffset_[slotId-1];
}

EutranConfigData* EutranConfigData::setMinRxLev(uint8_t slotId, int16_t minRxLev)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return this;
    }
    EMLogInfo("BBU[%d]: Working mode changes from[%d] to [%d]", static_cast<uint32_t>(slotId), static_cast<uint32_t>(minRxLev_[slotId-1]), minRxLev);
    minRxLev_[slotId-1] = minRxLev;
    return this;
}

int16_t EutranConfigData::getMinRxLev(uint8_t slotId)
{
    if( !BbuManagerPtr->isSlotIdValid(slotId) )
    {
        return 0;
    }
    return minRxLev_[slotId-1];
}

